/*
 * DiagSaver application
 *
 * Marvell Diag log saver on Linux - 1.0.0.0
 *
 * Copyright (C) 1995-2014 Marvell.Co.,LTD
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/stat.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>

#include <netinet/in.h>
#include <netdb.h>

#include <libgen.h>
#include <sys/inotify.h>

#include <poll.h>
/* #include <libudev.h> */

#include "usb_find.h"


#define _FILE_SIZE 10 //10MB
#define _FILE_SIZE_CP 200 //10MB


#define EXIT(name)          \
    do                      \
    {                       \
        perror(name);       \
        exit(EXIT_FAILURE); \
    } while (0)

char filename[256] = {0};
	
char filename1[256] = {0};
char path0[256] = "/dev/";
char pathtest[256] = "/dev/";

char path_at[256] = "/dev/";
char path_download[256] = "/dev/";

FILE *fp = NULL;
FILE *fpcp = NULL;

int sockfd;
int useSocket;
int fd;
int fdcp;
int fdat;
char *fileName;
#define CATCH_CP_LOG 1
 struct CH_TIMESTAMP {
 unsigned char sync;
 unsigned char lenM;
 unsigned char lenL;
 unsigned char flowid;
 unsigned int date;
 unsigned int ms;
};

unsigned int g_uiFileIndex = 0x0;

/* struct udev *udevp;
struct udev_monitor *monp;
int monitorfd;
char udevpath[256] = {0}; */

struct CSDLFileHeader
{
    unsigned int dwHeaderVersion; //0x0
    unsigned int dwDataFormat;    //0x1
    unsigned int dwAPVersion;
    unsigned int dwCPVersion;
    unsigned int dwSequenceNum;
    unsigned int dwTime;     //Total seconds from 1970.1.1 0:0:0
    unsigned int dwCheckSum; //0x0
};

int openport(char *pszDeviceName)
{
    int fd = open(pszDeviceName, O_RDWR | O_SYNC | O_NOCTTY | O_NDELAY);
    if (-1 == fd)
    {
        printf("Can't Open Diag device! %s",pszDeviceName);
        return -1;
    }
    else
    {
        ////tprintf (_T("\nOpened Diag DeviceName : %s\n"),pszDeviceName);
        return fd;
    }
}




void ctrl_c_process(int signo)
{
    printf("%s %s Got SIGINT\n", __FILE__, __FUNCTION__);
    // raise(SIGTERM);
    if (useSocket)
    {
        close(sockfd);
    }
    else
    {
        printf("close FP\n");
        fclose(fp);
    }
    printf("Exiting DiagSaver...\n");
    exit(1);
}

void *devname(unsigned int pid , unsigned int vid , unsigned int device_interface_id,unsigned int type)
{
    printf("%s %s\n", __FILE__, __FUNCTION__);
    const char *name = FindUsbDevice(pid,vid,device_interface_id,type);
    if (name == NULL )
    {
        return NULL	;
    }

    
}

int c2i(char ch)
{
        // 如果是数字，则用数字的ASCII码减去48, 如果ch = '2' ,则 '2' - 48 = 2
        if(isdigit(ch))
                return ch - 48;
 
        // 如果是字母，但不是A~F,a~f则返回
        if( ch < 'A' || (ch > 'F' && ch < 'a') || ch > 'z' )
                return -1;
 
        // 如果是大写字母，则用数字的ASCII码减去55, 如果ch = 'A' ,则 'A' - 55 = 10
        // 如果是小写字母，则用数字的ASCII码减去87, 如果ch = 'a' ,则 'a' - 87 = 10
        if(isalpha(ch))
                return isupper(ch) ? ch - 55 : ch - 87;
 
        return -1;
}
 
/*
 * 功能：将十六进制字符串转换为整型(int)数值
 * */
unsigned int  hex2dec(char *hex)
{
        int len;
        unsigned int num = 0;
        unsigned int temp;
        int bits;
        int i;
        
        // 此例中 hex = "1de" 长度为3, hex是main函数传递的
        len = strlen(hex);
 
        for (i=0, temp=0; i<len; i++, temp=0)
        {
                // 第一次：i=0, *(hex + i) = *(hex + 0) = '1', 即temp = 1
                // 第二次：i=1, *(hex + i) = *(hex + 1) = 'd', 即temp = 13
                // 第三次：i=2, *(hex + i) = *(hex + 2) = 'd', 即temp = 14
                unsigned int temp  = c2i( *(hex + i) );
                // 总共3位，一个16进制位用 4 bit保存
                // 第一次：'1'为最高位，所以temp左移 (len - i -1) * 4 = 2 * 4 = 8 位
                // 第二次：'d'为次高位，所以temp左移 (len - i -1) * 4 = 1 * 4 = 4 位
                // 第三次：'e'为最低位，所以temp左移 (len - i -1) * 4 = 0 * 4 = 0 位
                bits = (len - i - 1) * 4;
                temp = temp << bits;
 
                // 此处也可以用 num += temp;进行累加
                num = num | temp;
        }
 
        // 返回结果
        return num;
}


int main(int argc, char **argv)
{
    if(argc >= 4)
    {
        unsigned int pid  = hex2dec(argv[1]);
        unsigned int vid  = hex2dec(argv[2]);
        unsigned int interface_id =  hex2dec(argv[3]);
        unsigned int type = hex2dec(argv[4]);
        printf("%s %s %x %x %x %x\n", __FILE__, __FUNCTION__,pid,vid,interface_id,type);
        devname(pid,vid,interface_id,type);
    }
    else
    {
        printf("argc number err\n");
    }
    return 0;
}
